<?php
namespace Admin\Controller;

use Admin\Util\lib\API;
use Admin\Util\lib\YZTokenClient;
use Util\AccessController;
use Admin\Util\lib\YouZanApiFunction;

class SBookController extends AccessController {


    public function Index() {
        $LS = Service('M', 'sbook');
        $where = array();
        $_GET['TITLE'] ? $where['TITLE'] = array('like',
            '%' . $_GET['TITLE'] . '%') : 1;
        $_GET['ISBN'] ? $where['ISBN'] = $_GET['ISBN'] : 1;
        $_GET['AUTHOR'] ? $where['AUTHOR'] = array('like',
            '%' . $_GET['AUTHOR'] . '%') : 1;
        $_GET['PRESS'] ? $where['PRESS'] = array('like',
            '%' . $_GET['PRESS'] . '%') : 1;
        $_GET['PACKING_SPECIFICATION'] ? $where['PACKING_SPECIFICATION'] = $_GET['PACKING_SPECIFICATION'] : 1;


        //书籍包装
        $LS1 = Service('Book', 'region');
        $flpdata = $LS1->find(array('region_name' => '书籍包装'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->BOOK_BZ = $yjfldata;

        $count = $LS->count($where);
        $Page = new \Think\NewPage($count, 5);
        $show = $Page->show(); // 分页显示输出
        $limit = $Page->limit();
        $this->show = $show;

        $order = I('get.order') != '' && I('get.sort') != '' ? PageSort(I('get.order'), I('get.sort')) : 'SM_TIME desc';
        $files = 'ts_sbook.*,ts_u_user.user_wx_name';
        $join = 'left join ts_u_user on ts_u_user.user_id = ts_sbook.OPENID';
        $DATA = $LS->join($where, $join, $files, $limit, $order);
        $this->data = $DATA;
        $this->display();
    }

    public function AddSBook() {
        $LS = Service('Book', 'book');
        $data = $LS->find(array('ID' => $_POST['ID']));
        if ($data) {
            unset($data['ID']);
            unset($data['STATE']);
            $LS1 = Service('Book', 'sbook');
            $HOK = $LS1->find(array('TITLE' => $data['TITLE']));
            if (!$HOK) {
                $OK = $LS1->insert($data);
                echo $OK;
            } else {
                echo -1;
            }

        }
        echo 0;
    }

    /*
     * 编辑书籍index
     */
    public function EditBookIndex() {
        $LS = Service('Book', 'sbook');
        $data = $LS->find(array('ID' => $_GET['ID']));

        $this->BOOK_STATE = C('BOOK_STATE');
        //分类
        $LS1 = Service('Book', 'region');
        $flpdata = $LS1->find(array('region_name' => '童书分类'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->yjfldata = $yjfldata;

        //语言
        $YYpdata = $LS1->find(array('region_name' => '语言'));
        $YYsondata = $LS1->query(array('region_fid' => $YYpdata['region_id']));
        $this->BOOK_LANGUAGES = $YYsondata;

        //年龄段
        $YYpdata = $LS1->find(array('region_name' => '智能选书年龄段'));
        $YYsondata = $LS1->query(array('region_fid' => $YYpdata['region_id']));
        $this->BOOK_AGE = $YYsondata;

        //书籍成色
        $CSpdata = $LS1->find(array('region_name' => '书籍成色'));
        $CSsondata = $LS1->query(array('region_fid' => $CSpdata['region_id']));
        $this->BOOK_LEVEL = $CSsondata;

        //书籍包装
        $BZpdata = $LS1->find(array('region_name' => '书籍包装'));
        $BZsondata = $LS1->query(array('region_fid' => $BZpdata['region_id']));
        $this->BOOK_PACKING = $BZsondata;

        //仓库位置
        $LS2 = Service('Book', 'ck');
        $ckdata = $LS2->query();
        $this->CKDATA = $ckdata;

        //书籍规格
        $LS3 = Service('Book', 'book_gg');
        $ggdata[] = $LS3->find(array('BOOK_ID' => $_GET['ID'],
            'GG_NAME' => '全新'));
        $ggdata[] = $LS3->find(array('BOOK_ID' => $_GET['ID'],
            'GG_NAME' => '9.9成新'));
        $ggdata[] = $LS3->find(array('BOOK_ID' => $_GET['ID'],
            'GG_NAME' => '9.5成新'));
        $ggdata[] = $LS3->find(array('BOOK_ID' => $_GET['ID'],
            'GG_NAME' => '9成新'));
        $ggdata[] = $LS3->find(array('BOOK_ID' => $_GET['ID'],
            'GG_NAME' => '9成新以下'));
        $this->GGDATA = $ggdata;


        //书籍标签
        $flpdata = $LS1->find(array('region_name' => '书籍标签'));
        $yjfldata = $LS1->query(array('region_fid' => $flpdata['region_id']));
        $this->BOOK_BQ = $yjfldata;

        //显示书籍标签
        $LS2 = Service('Book', 'book_bq');
        $bqdata = $LS2->query(array('BOOK_ID' => $_GET['ID']), null, null, null, 'ts_region on ts_region.region_id = ts_book_bq.BQ_ID ');
        $this->BQDATA = $bqdata;
        $this->BQDATA_JSON = json_encode($bqdata);


        //默认书柜位置
        $LS4 = Service('Book', 'sg');
        $MRWZDATA = $LS4->GetWZ1($this->S('MRWZID'));
        $this->MRWZDATA = $MRWZDATA;
        $this->DATA = $data;
        $this->display();
    }


    /*
 * 编辑书籍
 */
    public function EditBook() {
        $LS = Service('Book', 'sbook');
        $update = $_POST;
        $_POST['PIC_URL']? $update['PIC_URL'] = $_POST['PIC_URL']:1;
        $update['details'] = preg_replace("/<img\s+[^>]*?src=(\'|\")(.*?)\\1[^>]*>/i", '<img src="$2" class="img-responsive center-block"/>', $_POST['CONTENT']);
        $LS1 = Service('Book', 'region');
        $region = $LS1->find(array('region_id' => $_POST['region_one']));
        $update['region_one'] = $region['region_name'] ? $region['region_name'] : $_POST['region_one'];
        //修改
        if ($_POST['ID']) {
            $where['ID'] = $_POST['ID'];
            $OK = $LS->update($where, $update);
        } else {
            $OK = $LS->insert($update);
        }

        if ($OK) {
            $_GET['BHCODE'] ? $this->success('编辑/添加成功', U('StoreHouse/Bookindex?BHCODE=' . $_GET['BHCODE'])) : $this->success('编辑成功');
        } else {
            $_GET['BHCODE'] ? $this->error('编辑/添加失败', U('StoreHouse/Bookindex?BHCODE=' . $_GET['BHCODE'])) : $this->error('编辑失败');
        }


    }

    /*
     * 删除书籍
     */
    public function delete() {
        $where['ID'] = $_POST['ID'];
        $LS = Service('Book', 'sbook');
        $OK = $LS->remove($where);
        echo $OK;
    }

    public function updateState() {
        $where['ID'] = $_POST['ID'];
        $save['STATE'] = $_POST['STATE'] == 1 ? -1 : 1;
        $LS = Service('Book', 'sbook');
        $OK = $LS->update($where, $save);
        echo $OK;
    }

    /*
     * 加入主库
     * 如果是扫码数据则加入下次扫码数据中
     */
    public function AddBook() {
        $LS = Service('M', 'sbook');
        $sbookdata = $LS->find(array('ID' => $_POST['ID']));
        $LS->Del(array('ID' => $_POST['ID']));
        unset($sbookdata['ID']);
        //加入黑名单
        if ($_POST['TOP'] == 1) {
            $sbookdata['IS_BLACK'] = 1;
            $log['TYPE'] = 2;
        } else {
            $sbookdata['IS_BLACK'] = 0;
            $log['TYPE'] = 1;
        }
        $LS1 = Service('M', 'book');
        $OK = $LS1->Add($sbookdata);
        //添加记录
        $log['ADMIN_ID'] = $this->S('ID');
        $log['BOOK_ID'] = $OK;
        $BL = A('Admin/BookLog');
        $BL->Add($log);
        //如果是扫码数据，则加入下次扫码卖书
        if ($sbookdata['OPENID']) {
            $LS2 = Service('M', 'sh_book');
            $where['ISBN'] = $sbookdata['ISBN'];
            $where['OPENID'] = $sbookdata['OPENID'];
            $save1['SHTIME'] = date('Y-m-d H:i:s');
            $save1['INFO'] = $_POST['TOP'] ? $this->S('USER_NAME') . '加入黑名单' : $this->S('USER_NAME') . '加入库存';
            $save1['BOOK_ID'] = $OK;
            $LS2->update($where, $save1);
        }
        echo $OK;

    }

    /*
    *导出当当链接
    */

    public function UpdatePicIndex() {
        $LS = Service('Book', 'book');
        $where['PIC_URL'] = array('EXP',
            'IS NULL');
        $where['PIC_URL'] = '';
        $condition['_logic'] = 'OR';
        $data = $LS->query($where, null, 'ISBN,ID');
        $urls = array();
        foreach ($data as $k => $v) {
            $url['ISBN'] = 'http://search.dangdang.com/?key=' . $v['ISBN'] . '&ID=' . $v['ID'];
            $urls[] = $url;
        }
        export($urls, array('ISBN'));
        $this->display();
    }


}